<template>
  <pro-layout
    :title="title"
    :menus="menus"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :logo="logoRender"
    :i18nRender="i18nRender"
    v-bind="settings"
    :siderWidth="208"
  >
    
    <!--自定义侧边框，覆盖上面的pro-layout中的侧边框
    <template v-slot:menuRender>
      <GlobalSider :menus="menus" :theme="settings.theme" :collapsed="collapsed"></GlobalSider>
    </template>
    -->

    <!--系统设置功能-->
    <setting-drawer ref="setDrawer" :settings="settings" @change="handleSettingChange" />
    <template v-slot:rightContentRender>
      <right-content  @handleToSettings="handleToSettings" :top-menu="settings.layout === 'topmenu'" :is-mobile="isMobile" :theme="settings.theme" />
    </template>
    <template v-slot:footerRender>
        <span></span>
      <!-- <global-footer /> -->
    </template>
    <MultiTab></MultiTab>
    <!-- <div class="sys-right-content">
        <RouteView></RouteView>
    </div> -->
    <RouteView></RouteView>
  </pro-layout>
</template>

<script>
//自己写的切换颜色
import SettingDrawer from '@/components/SettingDrawer'
import {initTheme} from '@/components/SettingDrawer/settingConfig'
import { i18nRender } from '@/locales'
import { mapState } from 'vuex'
import { SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'
import RouteView from '@/layouts/RouteView'
import defaultSettings from '@/config/defaultSettings'
import RightContent from '@/components/GlobalHeader/RightContent'
import GlobalFooter from '@/components/GlobalFooter'
import GlobalSider from '@/components/GlobalSider'
import MultiTab from '@/components/MultiTab'
import logo from '@/assets/logo.png'

export default {
  name: 'BasicLayout',
  components: {
    SettingDrawer,
    RightContent,
    GlobalFooter,
    GlobalSider,
    MultiTab,
    RouteView
  },
  data () {
    return {
      // preview.pro.antdv.com only use.
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      // end

      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      title: defaultSettings.title,
      settings: {
        // 布局类型
        layout: defaultSettings.layout, // 'sidemenu', 'topmenu'
        // 定宽: true / 流式: false
        contentWidth: defaultSettings.layout === 'sidemenu' ? false : defaultSettings.contentWidth === 'Fixed',
        
        theme: defaultSettings.navTheme,
        // 主色调
        primaryColor: localStorage.getItem(defaultSettings.localStoragePrimaryColorKey) || defaultSettings.primaryColor,
        fixedHeader: defaultSettings.fixedHeader,
        fixSiderbar: defaultSettings.fixSiderbar,
        colorWeak: defaultSettings.colorWeak,
        screenWeak:defaultSettings.screenWeak,
        hideHintAlert: true,
        hideCopyButton: true,
      },
      // 媒体查询
      query: {},

      // 是否手机模式
      isMobile: false,
    
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.addRouters
    })
   
  },
  created () {
    
    const routes = this.mainMenu.find(item => item.path === '/')
    // if(routes && routes.children){
    //     this.menus = routes.children.filter(item=>{
    //         return item["name"].indexOf("page")<0
    //     })
    // }
    this.menus = (routes && routes.children) || []
    // 处理侧栏收起状态
    this.$watch('collapsed', () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    })
    this.$watch('isMobile', () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    })

    
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }

     //初始化主题
    this.initTheme()
    
  },
  methods: {
    i18nRender,
    handleMediaQuery (val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.collapsed = false
        this.settings.contentWidth = false
     
      }
    },
    handleCollapse (val) {
      this.collapsed = val
    },
    handleSettingChange ({ type, value }) {
      type && (this.settings[type] = value)
      switch (type) {
        case 'contentWidth':
          this.settings[type] = value === 'Fixed'
          break
        case 'layout':
          if (value === 'sidemenu') {
            this.settings.contentWidth = false
          } else {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = true
          }
          break
        case 'navTheme':
          this.settings.theme=value
          break
        case 'primaryColor':
          this.settings.primaryColor=value
          defaultSettings.primaryColor=value
          break
        case 'fixedHeader':
          this.settings.fixedHeader=value
          break
        case 'fixSiderbar':
          this.settings.fixSiderbar=value
          break
        case 'screenWeak':
            //大屏模式
      
          this.settings.screenWeak=value
         
          break
      }
    },
    logoRender () {
     // return <LogoSvg />
      return <img src={logo}/>
   //  return <img src={logo}/>
    },
    handleToSettings(){
      //使用系统的颜色切换，调用此方法
      // this.$refs['setDrawer'].setShow(true)

      //使用自己写的颜色切换，调用此方法
      this.$refs['setDrawer'].showDrawer()
     
      
    },
    initTheme(){
        let theme = localStorage.getItem(defaultSettings.localStorageThemeKey)
        initTheme(theme || defaultSettings.primaryTheme)
    }
  }
}
</script>

<style lang="less">
    @import "./BasicLayout.less";
</style>

<style scoped>
    .sys-right-content {
        min-height: calc(100vh - 115px);
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        margin: 0;
        padding: 0;
        color: rgba(0, 0, 0, 0.65);
        font-size: 14px;
        font-variant: tabular-nums;
        line-height: 1.5;
        list-style: none;
        -webkit-font-feature-settings: 'tnum';
        font-feature-settings: 'tnum';
        position: relative;
        background: #fff;
        border-radius: 2px;
        -webkit-transition: all 0.3s;
        transition: all 0.3s;
    }
</style>
